﻿/*
	Copyright 2009 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

/*
Music Functions
*/

function music(){

	var nowPlayingList;
	var curAudioClip=null;
	var curTrackNo = -1; // -1 = no track loded
	var playTrackList;

/**************************************************************
	play track and setup track information
*************************************************************/
	music.prototype.playTrack = function(trackNo){

	//when end of tracklist is reached, beginn again
		if(this.playTrackList.trackList[trackNo] == null){
			trackNo = 0;
		}
		this.curTrackNo = trackNo;	

	// get information for playing
		var playURL = this.playTrackList.trackList[trackNo].playURL + partnertoken;
		var trackTitle = this.playTrackList.trackList[trackNo].trackTitle;
		var trackArtist = this.playTrackList.trackList[trackNo].artistName;
		var trackLength = this.playTrackList.trackList[trackNo].trackLength;

	// if no other clip is playing  play else stop other clip and play then
		if(this.curAudioClip == null){

		// show current track title in listbox and enable pause/stop buttons
			if(listboxChanged == false){
				anim.adjustMaintoTrack(trackNo, lbxMain, anim.lbxMainItems, dvMain, sbVertical);
			}
			btnPause.enabled = true;
			btnStop.enabled = true;
			btnNextTrack.enabled = true;
			btnPrevTrack.enabled = true;

			pbSong.visible = true;
			pbSong.value = 0;
			clearInterval(anim.progressAnimation);
			pbSong.max = (trackLength - trackLength % 1000) / 1000; // whith 1000 = 1 sec. progressbar has no stops

		//play current track
			this.curAudioClip=framework.audio.play(playURL, this.onAudioStateChange);
			this.setVolume();

		//start progressAnimation
			anim.progressAnimation = view.setInterval(function() {anim.showTrackStatus(pbSong);}, 1000);

		// show album art
		if(lastList != "TrackToken"){
			if(this.playTrackList.trackList[trackNo].albumArtURL != null){
				mp3t.sendRequest(this.playTrackList.trackList[trackNo].albumArtURL, true);
			} 
		} else {
			mp3t.mp3_getLockerData("track", "", "", this.playTrackList.trackList[trackNo].albumId, "", "", "","","music.showCover"); //because when all tracks displayed never albumArt returned
		}

		}else{
			this.curTrackNo = -1;
			this.curAudioClip.stop();
			this.curAudioClip=null;
			this.playTrack(trackNo);
		}
	}


/**************************************************************
 second albumArt request because when all tracks 
	are displayed, no albumArt is returned
*************************************************************/
	music.prototype.showCover = function(obj){
		if(obj.trackList[0].albumArtURL!=null){
			mp3t.sendRequest(obj.trackList[0].albumArtURL, true);
		}
	}

/**************************************************************
	(un-)pause a track
	(ajust progressbar on restart)
*************************************************************/
	music.prototype.pauseTrack = function(){
		if(this.curAudioClip.state==gddSoundStatePlaying){
			this.curAudioClip.pause();
			btnPause.enabled = false;
			btnPause.visible = false;
			btnPlay.visible = true;
			btnPlay.enabled = true;
			clearInterval(anim.progressAnimation);
		} else if(this.curAudioClip.state == gddSoundStatePaused){
			this.curAudioClip.play();
			btnPlay.enabled = false;
			btnPlay.visible = false;
			btnPause.visible = true;
			btnPause.enabled = true;
			pbSong.value = this.curAudioClip.currentPosition;
			anim.progressAnimation = view.setInterval(function() {anim.showTrackStatus(pbSong);}, 1000);
		}	
	}

/**************************************************************
	stop track
*************************************************************/
	music.prototype.stopTrack = function(){
		if(this.curAudioClip.state == gddSoundStatePlaying  || this.curAudioClip.state == gddSoundStatePaused){
			this.curTrackNo = -1;
			btnPause.enabled = false;
			btnStop.enabled = false;
			btnPrevTrack.enabled = false;
			btnNextTrack.enabled = false;
			this.curAudioClip.stop();
	}
}

/**************************************************************
	reset everything after a track stopped
*************************************************************/
	music.prototype.onAudioStateChange = function(audioClip, state) {
		if (state == gddSoundStateStopped) {
			if(lbxMain.selectedItem != null){
				lbxMain.selectedItem.selected = false;
			}
			hideAlbumArt(0);
			clearInterval(anim.progressAnimation);
			pbSong.visible = false;
			dvVolume.visible = false;
			imgAlbumcover.visible = false;
			imgAlbumcover.enabled = false;
			imgAlbumcover.opacity = 255;
			imgAlbumcover.src = "";
			music.curAudioClip = null;
			if(music.curTrackNo != -1){
				music.playTrack(music.curTrackNo + 1);
			}
		}  else if(state == gddSoundStatePlaying) {
			dvVolume.visible = true;
		}
	}

/**************************************************************
	change volume
*************************************************************/
	music.prototype.setVolume = function(){
		if(this.curAudioClip != null){
			this.curAudioClip.volume = sbVolume.value;
			options.putValue("Volume", sbVolume.value);
		}
	}
}

/**************************************************************
	go to previous track
*************************************************************/
function previousTrack(){
	if(music.curTrackNo > 0){
		setTimeout(function() {music.playTrack(music.curTrackNo - 1); }, 50);
	}
}

/**************************************************************
	go to next track
*************************************************************/
function nextTrack(){
	setTimeout(function() {music.playTrack(music.curTrackNo + 1);}, 50);
}

function playTrackInit(trackNo){
	music.playTrackList = lastTrackList;
	setTimeout(function() {music.playTrack(trackNo); },50);
	listboxChanged = false;
}
